package com.ihai.spring.boot.sample.httpclient;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import javax.net.ssl.*;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpsTest {
    public static void main(String[] args) throws Exception {
        System.setProperty("javax.net.debug", "all");
        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
        String keypath = "E:\\普元\\ESB\\定制需求\\NIG服务治理项目（尼日利亚 ）\\HTTPS\\Client cert\\star_9mobile_com_ng_22643214\\client.keystore";
//        String keypath = "E:\\ssl\\client.jks";
        String pwd = "000000";
        //初始化KeyManager
        KeyManagerFactory keyFactory = null;
        try {
            keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            KeyStore keystore = KeyStore.getInstance("JKS");
            keystore.load(new FileInputStream(new File(keypath)), pwd.toCharArray());
            keyFactory.init(keystore, pwd.toCharArray());

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        KeyManager[] keyManagers = keyFactory.getKeyManagers();
        //初始化Trust Manager
        TrustManagerFactory trustFactory = null;

        try {
            trustFactory = TrustManagerFactory.getInstance("SunX509");
        } catch (NoSuchAlgorithmException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        KeyStore tsstore;
        try {
            tsstore = KeyStore.getInstance("JKS");
            tsstore.load(new FileInputStream(new File(keypath)), pwd.toCharArray());
            trustFactory.init(tsstore);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        TrustManager[] trustManagers = new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType)
                            throws CertificateException {
                    }
                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType)
                            throws CertificateException {
                    }
                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                }
        };
        //注册HtpClient
        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance("SSLv3");
            sslContext.init(keyManagers, trustManagers, null);
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (KeyManagementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //设置规则限制
        SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(sslContext,
                new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"}, null, null);
        //注册
        Registry<ConnectionSocketFactory> socketFactoryRegistry = null;
        socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.INSTANCE)
                .register("https", ssf).build();
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        CloseableHttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connManager).build();
        HttpGet get = new HttpGet("https://9mobile.com.ng:9090/s");
        httpClient.execute(get);
    }
}
